package cn.edu.xmu.oomall.customer.dao.bo;


import cn.edu.xmu.javaee.core.aop.CopyFrom;
import cn.edu.xmu.javaee.core.model.bo.OOMallObject;
import cn.edu.xmu.javaee.core.model.dto.UserDto;
import cn.edu.xmu.oomall.customer.controller.dto.AddressDto;
import cn.edu.xmu.oomall.customer.dao.AddressDao;
import cn.edu.xmu.oomall.customer.dao.openfegin.RegionDao;
import cn.edu.xmu.oomall.customer.mapper.po.AddressPo;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.time.LocalDateTime;

@NoArgsConstructor
@AllArgsConstructor
@ToString(callSuper = true, doNotUseGetters = true)
@JsonInclude(JsonInclude.Include.NON_NULL)
@CopyFrom({AddressPo.class, AddressDto.class})
public class Address extends OOMallObject implements Serializable {

    //默认
    @ToString.Exclude
    @JsonIgnore
    public static final Byte IS_DEFAULT = 1;
    //普通地址
    @ToString.Exclude
    @JsonIgnore
    public static final Byte NOT_DEFAULT = 0;

    private Long customerId;

    private Long regionId;

    private String address;

    private String consignee;

    private String mobile;

    private Byte beDefault;

    private AddressDao addressDao;

    @ToString.Exclude
    @JsonIgnore
    private Region region;

    public Region getRegion() {
        return region;
    }

    public void setRegion(Region region) {
        this.region = region;
    }

    public void setAddressDao(AddressDao addressDao) {
        this.addressDao = addressDao;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public Byte getBeDefault() {
        return beDefault;
    }

    public void setBeDefault(Byte beDefault) {
        this.beDefault = beDefault;
    }

    public String getConsignee() {
        return consignee;
    }

    public void setConsignee(String consignee) {
        this.consignee = consignee;
    }

    public Long getCustomerId() {
        return customerId;
    }

    public void setCustomerId(Long customerId) {
        this.customerId = customerId;
    }

    public String getMobile() {
        return mobile;
    }

    public void setMobile(String mobile) {
        this.mobile = mobile;
    }

    public Long getRegionId() {
        return regionId;
    }

    public void setRegionId(Long regionId) {
        this.regionId = regionId;
    }

    @Override
    public void setGmtCreate(LocalDateTime gmtCreate) {

    }

    @Override
    public void setGmtModified(LocalDateTime gmtModified) {

    }


    public void setDefaultAddress(UserDto user) {
        this.cancelDefaultAddress(user);
        this.beDefault = (this.beDefault.equals(NOT_DEFAULT)) ? IS_DEFAULT : NOT_DEFAULT;
        addressDao.save(this, user);
    }

    public void cancelDefaultAddress(UserDto user) {
        Address bo = addressDao.findByBeDefault(IS_DEFAULT);
        bo.setBeDefault(NOT_DEFAULT);
        if (bo.getId() == null) {
            return;
        }
        addressDao.save(bo, user);
    }
}
